package LeetCode;

public class LC123 {
    public static void main(String[] args) {

    }
    public int maxProfit(int[] prices) {
        // 一共限制买两次，所以每天结束后可能有四种状态 1. 买了1个，buy1 2.买了1个又卖了 sell1 3.买了一个又卖了，之后又买一个buy2 4.卖买了两次 sell2
        int buy1 = -prices[0], sell1 = 0 , buy2 = -prices[0] , sell2 = 0;
        for (int i = 1; i < prices.length; i++) {
            buy1 = Math.max(buy1,-prices[0]);
            sell1 = Math.max(sell1,buy1 + prices[0]);
            buy2 = Math.max(buy2,sell1 - prices[0]);
            sell2 = Math.max(sell2 , buy2 + prices[0]);
        }
        return sell2;
    }
}
